package bidsanity

import grails.util.Environment

class TestdataController {

    def listingService

    def index() {
        [customerInstanceTotal: Customer.count(), bidInstanceTotal: Bid.count(), listingInstanceTotal: Listing.count()]
    }

    def clear() {
        //clear data
        Bid.executeUpdate("delete bidsanity.Bid ")
        Listing.executeUpdate("delete bidsanity.Listing")
        Customer.executeUpdate("delete bidsanity.Customer where isAdmin = false")

        if (session.user != null){ if (!Customer.findByEmailAddress(session.user).isAdmin) {session.user = null} }

        flash.message = "Data Cleared!"
        redirect(action: "index")
    }

    def build() {
        //clear data first
        Bid.executeUpdate("delete bidsanity.Bid ")
        Listing.executeUpdate("delete bidsanity.Listing")
        Customer.executeUpdate("delete bidsanity.Customer where isAdmin = false")

        def env = "unknown"
        if (Environment.current == Environment.DEVELOPMENT) { env = "dev"}
        else if (Environment.current == Environment.TEST) { env = "test" }
        else if (Environment.current == Environment.PRODUCTION) { env = "prod" }
        else { env = Environment.current.toString().toLowerCase()}
        String domain = "${env}.bidsanity.net"

        def bob = new Customer(emailAddress: "Bob@${domain}", password: 'testtest').save()
        def jim = new Customer(emailAddress: "Jim@${domain}", password: 'testtest').save()
        def joe = new Customer(emailAddress: "Joe@${domain}", password: 'testtest').save()
        def tim = new Customer(emailAddress: "Tim@${domain}", password: 'testtest').save()
        def zach = new Customer(emailAddress: "Zach@${domain}", password: 'testtest').save()
        def jeff = new Customer(emailAddress: "Jeff@${domain}", password: 'testtest').save()
        def john = new Customer(emailAddress: "John@${domain}", password: 'testtest').save()
        def scott = new Customer(emailAddress: "Scott@${domain}", password: 'testtest').save()

        for (i in 1..5){
            def listingParams = [name: "Bob's Listing #${i}", endDate:new Date() + i, startingBidPrice: i * 2,
                    description:  "Bob ${i}", sellerId: bob.id, winner:  null, startDate:  new Date() - 2]
            def l = listingService.createListing(listingParams)
            def dt = new Date()+ i - 1
            def cal = Calendar.getInstance()
            cal.setTime(dt)
            cal.set(Calendar.HOUR_OF_DAY, i)
            cal.set(Calendar.MINUTE, 30)
            cal.set(Calendar.SECOND, 26)

            l.addToBids(new Bid(listing: l, amount: i * 2 + 0.5, bidder: zach, bidTime: cal.getTime()))
            l.save()
        }

        new Bid(amount: 101.0D, bidder: bob, bidTime:  new Date() - 11,
                listing: new Listing(name: "Jim's Listing", endDate: new Date() - 10, startingBidPrice: 100.0D,
                description: 'a test listing',seller: jim, winner: null, startDate:  new Date() - 1).save()).save()
        new Bid(amount: 101.0D, bidder: bob, bidTime:  new Date() - 11,
                listing: new Listing(name: "Joe's Listing", endDate: new Date() - 10, startingBidPrice: 100.0D,
                description: 'a test listing',seller: joe, winner: null, startDate:  new Date() - 1).save()).save()
        new Bid(amount: 101.0D, bidder: bob, bidTime:  new Date() - 11,
                listing: new Listing(name: "Tim's Listing", endDate: new Date() - 10, startingBidPrice: 100.0D,
                description: 'a test listing',seller: tim, winner: null, startDate:  new Date()).save()).save()
        def ZachListing = new Listing(name: "A Completed Listing", endDate: new Date() - 32, startingBidPrice: 100.0D,
                description: 'completed',seller: zach, winner: tim, startDate: new Date() - 36).save(validate: false)
        for (i in 0..12){
            if(i%2 == 0){
                new Bid(amount: 100.0D + i, bidder: scott, bidTime:  new Date() - (34 - i),
                        listing: ZachListing).save()
            }
            else{
                new Bid(amount: 100.0D + i, bidder: tim, bidTime:  new Date() - (34 - i),
                        listing: ZachListing).save()
            }
        }
        new Bid(amount: 11500.0D, bidder: bob, bidTime:  new Date() + 11,
                listing: new Listing(name: "A no description listing", endDate: new Date() + 10, startingBidPrice: 100.0D,
                        seller: tim, winner: null, startDate:  new Date()).save()).save()
        use( [groovy.time.TimeCategory] ){
        //a listing with no bids
            def listingParams = [name: "No Bid Listing", endDate: new Date() + 2.minutes, startingBidPrice: 2700,
                description: "a listing without bids", sellerId: john.id, winner: null]
            listingService.createListing(listingParams).save()
        }


        if (session.user != null){ if (!Customer.findByEmailAddress(session.user).isAdmin) {session.user = null} }

        flash.message = "Data built!"
        redirect(action: "index")
    }

}
